<template>
  <div class="wrapper">
    <!-- header部分 -->
    <header>
      <p>商家信息</p>
    </header>
    <!-- 返回按钮 -->
    <div class="back-button" @click="goBack">
      <i class="fa fa-arrow-left"></i>
      <!-- 使用 Font Awesome 图标 -->
    </div>
    <!-- 商家logo部分 -->
    <div class="business-logo">
      <img :src="business.businessImg" />
    </div>

    <!-- 商家信息部分 -->
    <div class="business-info">
      <h1>{{ business.businessName }}</h1>
      <p>&#165;{{ business.starPrice }}起送 &#165;{{ business.deliveryPrice }}配送</p>
      <p>{{ business.businessExplain }}</p>
    </div>

    <!-- 食品列表部分 -->
    <ul class="food">
      <li v-for="item in foodList" :key="item.foodId">
        <div class="food-left">
          <img :src="item.foodImg" />
          <div class="food-left-info">
            <h3>{{ item.foodName }}</h3>
            <p>{{ item.foodExplain }}</p>
            <p>&#165;{{ item.foodPrice }}</p>
            <!-- 如果是会员用户 -->
            <p v-if="user.vip != 0">&#165;会员价：{{ getVipPrice(item.foodPrice) }}</p>
          </div>
        </div>
        <div class="food-right">
          <div>
            <i class="fa fa-minus-circle" @click="subOrUpdate(item.foodId, item.quantity)"></i>
          </div>
          <p>
            <span>{{ item.quantity }}</span>
          </p>
          <div>
            <i class="fa fa-plus-circle" @click="addOrUpdate(item.foodId, item.quantity)"></i>
          </div>
        </div>
      </li>
    </ul>

    <!-- 购物车部分 -->
    <div class="cart">
      <div class="cart-left">
        <div class="cart-left-icon">
          <i class="fa fa-shopping-cart"></i>
          <div class="cart-left-icon-quantity">{{ getCount }}</div>
        </div>
        <div class="cart-left-info">
          <p>&#165;{{ getToatal }}</p>
          <p>另需配送费{{ business.deliveryPrice }}元</p>
        </div>
      </div>
      <div class="cart-right">
        <!-- 不够起送费 -->

        <div class="cart-right-item2" v-if="getToatal < business.starPrice">&#165;{{ business.starPrice }}起送</div>

        <!-- 达到起送费 -->
        <div class="cart-right-item1" v-if="getToatal >= business.starPrice" @click="toOrder">去结算</div>
      </div>
    </div>
  </div>
</template>

<script>
import * as myConstants from '../const.js'
import { showDialog } from 'vant';

export default {
  data() {
    return {
      discount: myConstants.discount,
      businessId: this.$route.query.businessId,
      business: "", //商家信息
      foodList: [], //食品列表
      user: JSON.parse(sessionStorage.getItem("user")), //当前登录的用户信息
      cartList: [] //购物车列表
    };
  },
  //计算属性
  computed: {
    //统计购物车数量
    getCount() {
      let count = 0;
      for (let i = 0; i < this.foodList.length; i++) {
        count += this.foodList[i].quantity;
      }
      return count;
    },
    //总价:数量*单价再求和
    getToatal() {
      let total = 0;

      if (this.user.vip) {
        for (let i = 0; i < this.foodList.length; i++) {
          total += this.foodList[i].quantity * this.foodList[i].foodPrice * this.discount
        }
      }
      else {
        for (let i = 0; i < this.foodList.length; i++) {
          total += this.foodList[i].quantity * this.foodList[i].foodPrice
        }
      }
      return total;
    }
  },
  mounted() {
    //根据商家编号查询商家信息
    this.getBusinessInfo();
    //根据商家编号查询食品列表信息
    this.getFoodList();
  },
  methods: {

    // 计算会员价格（保留两位数）
    getVipPrice(price) {
      let VipPrice = price * this.discount
      return VipPrice.toFixed(2)
    },
    // 返回上一页
    goBack() {
      this.$router.go(-1);
    },
    toOrder() {
      this.$router.push({
        path: "/order",
        query: { businessId: this.businessId }
      });
    },
    //根据商家编号查询商家信息
    getBusinessInfo() {
      this.$http
        .get("/business/businessInfoById", {
          params: {
            id: this.businessId
          }
        })
        .then(res => {
          console.log(res)
          this.business = res.data.object;
        });
    },
    //根据商家编号查询食品列表信息
    async getFoodList() {
      await this.$http
        .get("/food/listFoodByBusinessId", {
          params: {
            businessId: this.businessId
          }
        })
        .then(res => {
          this.foodList = res.data.object;
          for (let i = 0; i < this.foodList.length; i++) {
            this.foodList[i].quantity = 0; //默认情况下：用户在当前商家未将任何食品添加到购物车
          }
          //查询购物车列表
          this.listCart();
        });
    },
    //点击+：添加或者更新购物车
    addOrUpdate(foodId, quantity) {
      if (quantity == 0) {
        this.addCart(foodId);
      } else {
        this.updateCart(foodId, quantity + 1);
      }
    },
    //点击-：删除或更新购物车
    subOrUpdate(foodId, quantity) {
      if (quantity == 0) {
        showDialog({
          message: '请先往购物车中添加数据',
        })
        } else if (quantity == 1) {
          this.deleteCart(foodId);
        } else {
        this.updateCart(foodId, quantity - 1);
      }
    },

    //API-添加购物车
    addCart(foodId) {
      // axios:post请求默认参数是application/json
      this.$http
        .post("/cart/addCart", {
          businessId: this.businessId,
          foodId: foodId,
          quantity: 1,
          userId: this.user.userId
        })
        .then(res => {
          // console.log(res);
          this.getFoodList();
        });
    },
    //API-更新购物车
    updateCart(foodId, quantity) {
      // axios:post请求默认参数是application/json
      this.$http
        .post("/cart/updateCart", {
          businessId: this.businessId,
          foodId: foodId,
          quantity: quantity,
          userId: this.user.userId
        })
        .then(res => {
          // console.log(res);
          this.getFoodList();
        });
    },
    //API-删除购物项
    deleteCart(foodId) {
      this.$http
        .post("/cart/deleteCart", {
          businessId: this.businessId,
          foodId: foodId,
          userId: this.user.userId
        })
        .then(res => {
          // console.log(res);
          this.getFoodList();
        });
    },
    //API-查询购物车列表
    async listCart() {
      await this.$http
        .get("/cart/listCart", {
          params: {
            businessId: this.businessId,
            userId: this.user.userId
          }
        })
        .then(res => {
          //需要比较购车列表和食品列表
          //如果foodList中的foodId在cartList中出现，说明当前食品被用户添加到了购物车，
          // 所以需要将foodList中quantity等同于cartList中当前购物项的quantity
          this.cartList = res.data.object;
          for (let i = 0; i < this.foodList.length; i++) {
            for (let j = 0; j < this.cartList.length; j++) {
              if (this.foodList[i].foodId == this.cartList[j].foodId) {
                this.foodList[i].quantity = this.cartList[j].quantity;
                break;
              }
            }
          }
        });
    }
  }
};
</script>

<style scoped>
/****************** 总容器 ******************/
.wrapper {
  width: 100%;
  height: 100%;
}

/****************** header部分 ******************/
.wrapper header {
  width: 100%;
  height: 12vw;
  background-color: #0097ff;
  color: #fff;
  font-size: 4.8vw;

  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;

  display: flex;
  justify-content: center;
  align-items: center;
}

/****************** 商家logo部分 ******************/
.wrapper .business-logo {
  width: 100%;
  height: 35vw;
  /*使用上外边距避开header部分*/
  margin-top: 12vw;

  display: flex;
  justify-content: center;
  align-items: center;
}

.wrapper .business-logo img {
  width: 40vw;
  height: 30vw;
  border-radius: 5px;
}

/****************** 商家信息部分 ******************/
.wrapper .business-info {
  width: 100%;
  height: 20vw;

  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.wrapper .business-info h1 {
  font-size: 5vw;
}

.wrapper .business-info p {
  font-size: 3vw;
  color: #666;
  margin-top: 1vw;
}

/****************** 食品列表部分 ******************/
.wrapper .food {
  width: 100%;
  /*使用下外边距避开footer部分*/
  padding-bottom: 28vw;
}

.wrapper .food li {
  width: 100%;
  box-sizing: border-box;
  padding: 2.5vw;
  user-select: none;

  display: flex;
  justify-content: space-between;
  align-items: center;
}

.wrapper .food li .food-left {
  display: flex;
  align-items: center;
}

.wrapper .food li .food-left img {
  width: 20vw;
  height: 20vw;
}

.wrapper .food li .food-left .food-left-info {
  margin-left: 3vw;
}

.wrapper .food li .food-left .food-left-info h3 {
  font-size: 3.8vw;
  color: #555;
}

.wrapper .food li .food-left .food-left-info p {
  font-size: 3vw;
  color: #888;
  margin-top: 2vw;
}

.wrapper .food li .food-right {
  width: 16vw;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.wrapper .food li .food-right .fa-minus-circle {
  font-size: 5.5vw;
  color: #999;
  cursor: pointer;
}

.wrapper .food li .food-right p {
  font-size: 3.6vw;
  color: #333;
}

.wrapper .food li .food-right .fa-plus-circle {
  font-size: 5.5vw;
  color: #0097ef;
  cursor: pointer;
}

/****************** 购物车部分 ******************/
.wrapper .cart {
  width: 100%;
  height: 14vw;

  position: fixed;
  left: 0;
  bottom: 13vw;

  display: flex;
}

.wrapper .cart .cart-left {
  flex: 2;
  background-color: #505051;
  display: flex;
}

.wrapper .cart .cart-left .cart-left-icon {
  width: 16vw;
  height: 16vw;
  box-sizing: border-box;
  border: solid 1.6vw #444;
  border-radius: 8vw;
  background-color: #3190e8;
  font-size: 7vw;
  color: #fff;

  display: flex;
  justify-content: center;
  align-items: center;

  margin-top: -4vw;
  margin-left: 3vw;

  position: relative;
}

.wrapper .cart .cart-left .cart-left-icon-quantity {
  width: 5vw;
  height: 5vw;
  border-radius: 2.5vw;
  background-color: red;
  color: #fff;
  font-size: 3.6vw;

  display: flex;
  justify-content: center;
  align-items: center;

  position: absolute;
  right: -1.5vw;
  top: -1.5vw;
}

.wrapper .cart .cart-left .cart-left-info p:first-child {
  font-size: 4.5vw;
  color: #fff;
  margin-top: 1vw;
}

.wrapper .cart .cart-left .cart-left-info p:last-child {
  font-size: 2.8vw;
  color: #aaa;
}

.wrapper .cart .cart-right {
  flex: 1;
}

/*达到起送费时的样式*/
.wrapper .cart .cart-right .cart-right-item1 {
  width: 100%;
  height: 100%;
  background-color: #38ca73;
  color: #fff;
  font-size: 4.5vw;
  font-weight: 700;
  user-select: none;
  cursor: pointer;

  display: flex;
  justify-content: center;
  align-items: center;
}

/*不够起送费时的样式（只有背景色和鼠标样式的区别）*/
.wrapper .cart .cart-right .cart-right-item2 {
  width: 100%;
  height: 100%;
  background-color: #535356;
  color: #fff;
  font-size: 4.5vw;
  font-weight: 700;
  user-select: none;

  display: flex;
  justify-content: center;
  align-items: center;
}

.back-button {
  position: fixed;
  top: 5vw;
  /* 根据需要调整高度 */
  left: 4vw;
  /* 给左边留出一定间距 */
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #ffffff;
  font-size: 4vw;
  z-index: 1000;
}
</style>